// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

using Elastic.Transport;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

#nullable restore
namespace Elastic.Clients.Elasticsearch.Security
{
	public sealed class SecurityGrantApiKeyRequestParameters : RequestParameters<SecurityGrantApiKeyRequestParameters>
	{
	}

	public sealed partial class SecurityGrantApiKeyRequest : PlainRequestBase<SecurityGrantApiKeyRequestParameters>
	{
		internal override ApiUrls ApiUrls => ApiUrlsLookups.SecurityGrantApiKey;
		protected override HttpMethod HttpMethod => HttpMethod.POST;
		protected override bool SupportsBody => true;
		[JsonInclude]
		[JsonPropertyName("api_key")]
		public Elastic.Clients.Elasticsearch.Security.GrantApiKey ApiKey { get; set; }

		[JsonInclude]
		[JsonPropertyName("grant_type")]
		public Elastic.Clients.Elasticsearch.Security.ApiKeyGrantType GrantType { get; set; }

		[JsonInclude]
		[JsonPropertyName("access_token")]
		public string? AccessToken { get; set; }

		[JsonInclude]
		[JsonPropertyName("username")]
		public Elastic.Clients.Elasticsearch.Username? Username { get; set; }

		[JsonInclude]
		[JsonPropertyName("password")]
		public string? Password { get; set; }
	}

	public sealed partial class SecurityGrantApiKeyRequestDescriptor : RequestDescriptorBase<SecurityGrantApiKeyRequestDescriptor, SecurityGrantApiKeyRequestParameters>
	{
		internal SecurityGrantApiKeyRequestDescriptor(Action<SecurityGrantApiKeyRequestDescriptor> configure) => configure.Invoke(this);
		public SecurityGrantApiKeyRequestDescriptor()
		{
		}

		internal override ApiUrls ApiUrls => ApiUrlsLookups.SecurityGrantApiKey;
		protected override HttpMethod HttpMethod => HttpMethod.POST;
		protected override bool SupportsBody => true;
		private string? AccessTokenValue { get; set; }

		private Elastic.Clients.Elasticsearch.Security.GrantApiKey ApiKeyValue { get; set; }

		private GrantApiKeyDescriptor ApiKeyDescriptor { get; set; }

		private Action<GrantApiKeyDescriptor> ApiKeyDescriptorAction { get; set; }

		private Elastic.Clients.Elasticsearch.Security.ApiKeyGrantType GrantTypeValue { get; set; }

		private string? PasswordValue { get; set; }

		private Elastic.Clients.Elasticsearch.Username? UsernameValue { get; set; }

		public SecurityGrantApiKeyRequestDescriptor AccessToken(string? accessToken)
		{
			AccessTokenValue = accessToken;
			return Self;
		}

		public SecurityGrantApiKeyRequestDescriptor ApiKey(Elastic.Clients.Elasticsearch.Security.GrantApiKey apiKey)
		{
			ApiKeyDescriptor = null;
			ApiKeyDescriptorAction = null;
			ApiKeyValue = apiKey;
			return Self;
		}

		public SecurityGrantApiKeyRequestDescriptor ApiKey(GrantApiKeyDescriptor descriptor)
		{
			ApiKeyValue = null;
			ApiKeyDescriptorAction = null;
			ApiKeyDescriptor = descriptor;
			return Self;
		}

		public SecurityGrantApiKeyRequestDescriptor ApiKey(Action<GrantApiKeyDescriptor> configure)
		{
			ApiKeyValue = null;
			ApiKeyDescriptor = null;
			ApiKeyDescriptorAction = configure;
			return Self;
		}

		public SecurityGrantApiKeyRequestDescriptor GrantType(Elastic.Clients.Elasticsearch.Security.ApiKeyGrantType grantType)
		{
			GrantTypeValue = grantType;
			return Self;
		}

		public SecurityGrantApiKeyRequestDescriptor Password(string? password)
		{
			PasswordValue = password;
			return Self;
		}

		public SecurityGrantApiKeyRequestDescriptor Username(Elastic.Clients.Elasticsearch.Username? username)
		{
			UsernameValue = username;
			return Self;
		}

		protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
		{
			writer.WriteStartObject();
			if (!string.IsNullOrEmpty(AccessTokenValue))
			{
				writer.WritePropertyName("access_token");
				writer.WriteStringValue(AccessTokenValue);
			}

			if (ApiKeyDescriptor is not null)
			{
				writer.WritePropertyName("api_key");
				JsonSerializer.Serialize(writer, ApiKeyDescriptor, options);
			}
			else if (ApiKeyDescriptorAction is not null)
			{
				writer.WritePropertyName("api_key");
				JsonSerializer.Serialize(writer, new GrantApiKeyDescriptor(ApiKeyDescriptorAction), options);
			}
			else
			{
				writer.WritePropertyName("api_key");
				JsonSerializer.Serialize(writer, ApiKeyValue, options);
			}

			writer.WritePropertyName("grant_type");
			JsonSerializer.Serialize(writer, GrantTypeValue, options);
			if (PasswordValue is not null)
			{
				writer.WritePropertyName("password");
				JsonSerializer.Serialize(writer, PasswordValue, options);
			}

			if (UsernameValue is not null)
			{
				writer.WritePropertyName("username");
				JsonSerializer.Serialize(writer, UsernameValue, options);
			}

			writer.WriteEndObject();
		}
	}
}